home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Games / Abalone 1.4.2 / src / Contest.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-09-21  |  5.8 KB  |  251 lines  |  [TEXT/MPS ]

  1. #define CONTEST_C
  2. #include "Contest.h"
  3. #undef CONTEST_C
  4.  
  5.  
  6. #if defined(__MWERKS__)
  7. #pragma segment __%Main
  8. #else
  9. #pragma segment Main
  10. #endif
  11.  
  12.  
  13. void
  14. MoveToContestNotation (MovePtr source, CoordinatePtr destination, BoardPtr board)
  15. {
  16.     if (source[1] != 0)
  17.         FlicheMoveToContestNotation (source, destination);
  18.     else if (board->field[Neighbour (source[0], source[3])] == board->field[source[0]])
  19.         NormalMoveToContestNotation (source, destination, board);
  20.     else
  21.         SimpleMoveToContestNotation (source, destination);
  22. }
  23.  
  24.  
  25.  
  26. ///void
  27. //ContestNotationToMove (CoordinatePtr source, MovePtr destination)
  28. //{
  29. //    if (source[3] == '-')    //    separator instead of head, so a simple move
  30. //        ContestNotationToSimpleMove (source, destination);
  31. //    else if (ContestNotationLineDirection (source) == ContestNotationMoveDirection (source))
  32. //        ContestNotationToNormalMove (source, destination);
  33. //    else    /* only fliche moves remain */
  34. //        ContestNotationToFlicheMove (source, destination);
  35. //}
  36.  
  37.  
  38.  
  39. void
  40. FieldToCoordinate (Field f, CoordinatePtr cp)
  41. {
  42.     char    h = 0, v = 0;
  43.         
  44.     if (f > 0 && f <= 5)    { h = (f -  1); v = 8; }
  45.     else if (f <= 11)        { h = (f -  6); v = 7; }
  46.     else if (f <= 18)        { h = (f - 12); v = 6; }
  47.     else if (f <= 26)        { h = (f - 19); v = 5; }
  48.     else if (f <= 35)        { h = (f - 27); v = 4; }
  49.     else if (f <= 43)        { h = (f - 36); v = 3; }
  50.     else if (f <= 50)        { h = (f - 44); v = 2; }
  51.     else if (f <= 56)        { h = (f - 51); v = 1; }
  52.     else if (f <= 61)        { h = (f - 57); v = 0; }
  53.     
  54.     cp[0] = 'a' + v;
  55.     cp[1] = '1' + h;
  56. }
  57.  
  58.  
  59.  
  60. Field
  61. CoordinateToField (CoordinatePtr coord)
  62. {
  63.     Assert (coord[0] >= 'a' && coord[0] <= 'i', ILLEGAL_PARAMETER);
  64.     Assert (coord[1] >= '0' && coord[1] <= '9', ILLEGAL_PARAMETER);
  65.     
  66.     switch (coord[0])
  67.     {
  68.         case 'a':
  69.         return coord[1] - '0' + 0;
  70.         case 'b':
  71.         return coord[1] - '0' + 5;
  72.         case 'c':
  73.         return coord[1] - '0' + 11;
  74.         case 'd':
  75.         return coord[1] - '0' + 18;
  76.         case 'e':
  77.         return coord[1] - '0' + 26;
  78.         case 'f':
  79.         return coord[1] - '0' + 35;
  80.         case 'g':
  81.         return coord[1] - '0' + 43;
  82.         case 'h':
  83.         return coord[1] - '0' + 50;
  84.         case 'i':
  85.         return coord[1] - '0' + 56;
  86.         default:
  87.         return 0;
  88.     }
  89. }
  90.  
  91.  
  92.  
  93. void
  94. SimpleMoveToContestNotation (MovePtr source, CoordinatePtr destination)
  95. {
  96.     Coordinate    oldtail, newtail;
  97.     
  98.     FieldToCoordinate (source[0], oldtail);
  99.     FieldToCoordinate (Neighbour (source[0], source[3]), newtail);
  100.     
  101.     destination[0] = oldtail[0];
  102.     destination[1] = oldtail[1];
  103.     destination[2] = '-';
  104.     destination[3] = newtail[0];
  105.     destination[4] = newtail[1];
  106.     destination[5] = '\0';
  107. }
  108.  
  109.  
  110.  
  111. void
  112. NormalMoveToContestNotation (MovePtr source, CoordinatePtr destination, BoardPtr board)
  113. {
  114.     Coordinate    oldtail, oldhead, newtail;
  115.     Field        current;
  116.     
  117.     FieldToCoordinate (source[0], oldtail);
  118.     
  119.     /* find old head */
  120.     
  121.     for (    current = source[0];
  122.             board->field[current] == board->field[Neighbour(current, source[3])];
  123.             current = Neighbour (current, source[3])
  124.         )
  125.         ;
  126.     
  127.     FieldToCoordinate (current, oldhead);
  128.         
  129. //    find new tail
  130.             
  131.     FieldToCoordinate (Neighbour(source[0], source[3]), newtail);
  132.     
  133.     destination[0] = oldtail[0];
  134.     destination[1] = oldtail[1];
  135.     destination[2] = oldhead[0];
  136.     destination[3] = oldhead[1];
  137.     destination[4] = '-';
  138.     destination[5] = newtail[0];
  139.     destination[6] = newtail[1];
  140.     destination[7] = '\0';
  141. }
  142.  
  143.  
  144.  
  145. void
  146. FlicheMoveToContestNotation (MovePtr source, CoordinatePtr destination)
  147. {
  148.     Coordinate    oldtail, oldhead, newtail;
  149.     
  150. //    The problem here is the 'always forward' convention, which is different from mine
  151.     
  152.     short flicheDirection = Direction (source[0], source[1]);
  153.     short difference = (source[3] - flicheDirection + 6) % 6;
  154.     
  155.     Assert (difference != 0 && difference != 3, ILLEGAL_PARAMETER);    //    that's a straight move!
  156.     
  157.     if (difference == 1 || difference == 5)         //    the fliche move is 'forward'
  158.     {
  159.         FieldToCoordinate (source[0], oldtail);
  160.         FieldToCoordinate (source[2] ? source[2] : source[1], oldhead);
  161.         FieldToCoordinate (Neighbour (source[0], source[3]), newtail);
  162.     }
  163.     else                                             //    the fliche move is 'backward'
  164.     {
  165.         /* This means the fliche should be read backward */
  166.         
  167.         FieldToCoordinate (source[2] ? source[2] : source[1], oldtail);
  168.         FieldToCoordinate (source[0], oldhead);
  169.         FieldToCoordinate (Neighbour (source[2] ? source[2] : source[1], source[3]), newtail);
  170.     }
  171.     destination[0] = oldtail[0];
  172.     destination[1] = oldtail[1];
  173.     destination[2] = oldhead[0];
  174.     destination[3] = oldhead[1];
  175.     destination[4] = '-';
  176.     destination[5] = newtail[0];
  177.     destination[6] = newtail[1];
  178.     destination[7] = '\0';
  179. }
  180.  
  181.  
  182.  
  183. short
  184. ContestNotationDirection (CoordinatePtr tail, CoordinatePtr head)
  185. {
  186.     Field    from = CoordinateToField (tail),
  187.             to = CoordinateToField (head);
  188.     short    d;
  189.             
  190.     if (Neighbours (from, to))    //    easy: line of 2 balls
  191.         return Direction (from, to);
  192.         
  193. //    tail and head are not neighbours, so this must be a line of three balls.
  194.     
  195.     for (d = right; d <= topright; d++)
  196.         if (Neighbour (Neighbour (from, d), d) == to)
  197.             return d;
  198.     
  199.     return down;
  200. }
  201.  
  202.  
  203.  
  204. short
  205. ContestNotationLineDirection (CoordinatePtr notation)
  206. {
  207.     Assert (notation[2] != '-', ILLEGAL_PARAMETER);
  208.     Assert (notation[4] == '-', ILLEGAL_PARAMETER);
  209.     
  210.     return ContestNotationDirection (notation, notation + 2);
  211. }
  212.  
  213.  
  214.  
  215. short
  216. ContestNotationMoveDirection (CoordinatePtr notation)
  217. {
  218.     return ContestNotationDirection (notation, notation + (notation[2] == '-' ? 3 : 5));
  219. }
  220.  
  221.  
  222.  
  223. //void
  224. //ContestNotationToSimpleMove (CoordinatePtr source, MovePtr destination)
  225. //{
  226. //#pragma unused (destination)
  227. //
  228. //    Assert (source[2] == '-', ILLEGAL_PARAMETER);
  229. //}
  230. //
  231. //
  232. //
  233. //void
  234. //ContestNotationToNormalMove (CoordinatePtr source, MovePtr destination)
  235. //{
  236. //#pragma unused (destination)
  237. //
  238. //    Assert (ContestNotationLineDirection (source) == ContestNotationLineDirection (source), INTERNAL_ERROR);
  239. //}
  240. //
  241. //
  242. //
  243. //void
  244. //ContestNotationToFlicheMove (CoordinatePtr source, MovePtr destination)
  245. //{
  246. //#pragma unused (destination)
  247. //
  248. //    Assert (ContestNotationLineDirection (source) != ContestNotationLineDirection (source), INTERNAL_ERROR);
  249. //}
  250. //
  251.